package fa7.socialcode.web;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ReverseComparator;

import fa7.socialcode.model.Usuario;
import fa7.socialcode.service.ConteudoManager;


/**
 * 
 * @author Paulo Sergio Girao
 *
 */

public class ConteudoList implements Serializable {
	
    private ConteudoManager conteudoManager;
   
    
    private String sortColumn = "id";
    private boolean ascending = true;

    public String getSortColumn() {
        return sortColumn;
    }

    public void setSortColumn(String sortColumn) {
        this.sortColumn = sortColumn;
    }

    public boolean isAscending() {
        return ascending;
    }

    public void setAscending(boolean ascending) {
        this.ascending = ascending;
    }
    
    public void setConteudoManager(ConteudoManager conteudoManager) {
		this.conteudoManager = conteudoManager;
	}
    
    private Usuario usuarioInSession;
    

	public Usuario getUsuarioInSession() {
		return usuarioInSession;
	}

	public void setUsuarioInSession(Usuario usuarioInSession) {
		this.usuarioInSession = usuarioInSession;
	}

	public List getConteudos(){
	   List conteudos = conteudoManager.findConteudosByUsuarioId(getUsuarioInSession().getId());
       //List conteudos = conteudoManager.findConteudos();

        Comparator comparator = null;

        if (sortColumn.equalsIgnoreCase("date")) {
            comparator = new BeanDateComparator(sortColumn);
        } else {
            comparator = new BeanComparator(sortColumn);
        }

        if (!ascending) {
            comparator = new ReverseComparator(comparator);
        }
        
        Collections.sort(conteudos, comparator);

        return conteudos;
    }
}
